/*
 * @(#)MtmUpdateAlertRecipient.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.alert.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import com.dcivision.alert.bean.MtmUpdateAlertRecipient;
import com.dcivision.alert.bean.UpdateAlert;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.bean.UserGroup;
import com.dcivision.user.bean.UserRole;

/**
  MtmUpdateAlertRecipientDAObject.java

  This class is the data access bean for table "MTM_UPDATE_ALERT_RECIPIENT".

  @author      Scott Tong
  @company     DCIVision Limited
  @creation date   21/08/2003
  @version     $Revision: 1.31.2.1 $
*/

public class MtmUpdateAlertRecipientDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.31.2.1 $";

  public static final String TABLE_NAME = "MTM_UPDATE_ALERT_RECIPIENT";

  public MtmUpdateAlertRecipientDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("UPDATE_ALERT_ID");
    this.vecDBColumn.add("UPDATE_ALERT_TYPE_ID");
    this.vecDBColumn.add("RECIPIENT_TYPE");
    this.vecDBColumn.add("RECIPIENT_ID");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.UPDATE_ALERT_ID, A.UPDATE_ALERT_TYPE_ID, A.RECIPIENT_TYPE, A.RECIPIENT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = new MtmUpdateAlertRecipient();
          tmpMtmUpdateAlertRecipient.setID(getResultSetInteger(rs, "ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertID(getResultSetInteger(rs, "UPDATE_ALERT_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertTypeID(getResultSetInteger(rs, "UPDATE_ALERT_TYPE_ID"));
          tmpMtmUpdateAlertRecipient.setRecipientType(getResultSetString(rs, "RECIPIENT_TYPE"));
          tmpMtmUpdateAlertRecipient.setRecipientID(getResultSetInteger(rs, "RECIPIENT_ID"));
          tmpMtmUpdateAlertRecipient.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmUpdateAlertRecipient.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmUpdateAlertRecipient.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUpdateAlertRecipient.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUpdateAlertRecipient.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmUpdateAlertRecipient.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getCreatorID()));
          tmpMtmUpdateAlertRecipient.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getUpdaterID()));
          return(tmpMtmUpdateAlertRecipient);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized AbstractBaseObject getByRecipientIDs(Integer recipientID,String recipientType,Integer update_alert_id,Integer update_alert_type_id) throws ApplicationException {
	    PreparedStatement preStat = null;
	    ResultSet rs = null;
	    StringBuffer sqlStat = new StringBuffer();

	    synchronized(dbConn) {
	      try {
	        sqlStat.append("SELECT A.ID, A.UPDATE_ALERT_ID, A.UPDATE_ALERT_TYPE_ID, A.RECIPIENT_TYPE, A.RECIPIENT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
	        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT A ");
	        sqlStat.append("WHERE  A.RECIPIENT_ID = ? AND A.RECIPIENT_TYPE = ? AND A.UPDATE_ALERT_ID = ? AND A.UPDATE_ALERT_TYPE_ID = ? ");
	        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        this.setPrepareStatement(preStat, 1, recipientID);
	        this.setPrepareStatement(preStat, 2, recipientType);
	        this.setPrepareStatement(preStat, 3, update_alert_id);
	        this.setPrepareStatement(preStat, 4, update_alert_type_id);
	        rs = preStat.executeQuery();
	        if (rs.next()) {
	          MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = new MtmUpdateAlertRecipient();
	          tmpMtmUpdateAlertRecipient.setID(getResultSetInteger(rs, "ID"));
	          tmpMtmUpdateAlertRecipient.setUpdateAlertID(getResultSetInteger(rs, "UPDATE_ALERT_ID"));
	          tmpMtmUpdateAlertRecipient.setUpdateAlertTypeID(getResultSetInteger(rs, "UPDATE_ALERT_TYPE_ID"));
	          tmpMtmUpdateAlertRecipient.setRecipientType(getResultSetString(rs, "RECIPIENT_TYPE"));
	          tmpMtmUpdateAlertRecipient.setRecipientID(getResultSetInteger(rs, "RECIPIENT_ID"));
	          tmpMtmUpdateAlertRecipient.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
	          tmpMtmUpdateAlertRecipient.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
	          tmpMtmUpdateAlertRecipient.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
	          tmpMtmUpdateAlertRecipient.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
	          tmpMtmUpdateAlertRecipient.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
	          tmpMtmUpdateAlertRecipient.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
	          tmpMtmUpdateAlertRecipient.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getCreatorID()));
	          tmpMtmUpdateAlertRecipient.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getUpdaterID()));
	          return(tmpMtmUpdateAlertRecipient);
	        } else {
	          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
	        }
	      } catch (ApplicationException appEx) {
	        throw appEx;
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	      } finally {
	        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	      }
	    }
	  }
 
  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.UPDATE_ALERT_ID, A.UPDATE_ALERT_TYPE_ID, A.RECIPIENT_TYPE, A.RECIPIENT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");

        // Form the WHERE clause for filtering.
        /*
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }
        */
        
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 2, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 2, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = new MtmUpdateAlertRecipient();
          tmpMtmUpdateAlertRecipient.setID(getResultSetInteger(rs, "ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertID(getResultSetInteger(rs, "UPDATE_ALERT_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertTypeID(getResultSetInteger(rs, "UPDATE_ALERT_TYPE_ID"));
          tmpMtmUpdateAlertRecipient.setRecipientType(getResultSetString(rs, "RECIPIENT_TYPE"));
          tmpMtmUpdateAlertRecipient.setRecipientID(getResultSetInteger(rs, "RECIPIENT_ID"));
          tmpMtmUpdateAlertRecipient.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmUpdateAlertRecipient.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmUpdateAlertRecipient.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUpdateAlertRecipient.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUpdateAlertRecipient.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmUpdateAlertRecipient.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getCreatorID()));
          tmpMtmUpdateAlertRecipient.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getUpdaterID()));
          tmpMtmUpdateAlertRecipient.setRecordCount(totalNumOfRecord);
          tmpMtmUpdateAlertRecipient.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpMtmUpdateAlertRecipient);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.UPDATE_ALERT_ID, A.UPDATE_ALERT_TYPE_ID, A.RECIPIENT_TYPE, A.RECIPIENT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = new MtmUpdateAlertRecipient();
          tmpMtmUpdateAlertRecipient.setID(getResultSetInteger(rs, "ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertID(getResultSetInteger(rs, "UPDATE_ALERT_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertTypeID(getResultSetInteger(rs, "UPDATE_ALERT_TYPE_ID"));
          tmpMtmUpdateAlertRecipient.setRecipientType(getResultSetString(rs, "RECIPIENT_TYPE"));
          tmpMtmUpdateAlertRecipient.setRecipientID(getResultSetInteger(rs, "RECIPIENT_ID"));
          tmpMtmUpdateAlertRecipient.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmUpdateAlertRecipient.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmUpdateAlertRecipient.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUpdateAlertRecipient.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUpdateAlertRecipient.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmUpdateAlertRecipient.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getCreatorID()));
          tmpMtmUpdateAlertRecipient.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getUpdaterID()));
          result.add(tmpMtmUpdateAlertRecipient);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = (MtmUpdateAlertRecipient)((MtmUpdateAlertRecipient)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   MTM_UPDATE_ALERT_RECIPIENT(ID, UPDATE_ALERT_ID, UPDATE_ALERT_TYPE_ID, RECIPIENT_TYPE, RECIPIENT_ID, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpMtmUpdateAlertRecipient.getUpdateAlertID());
        setPrepareStatement(preStat, 3, tmpMtmUpdateAlertRecipient.getUpdateAlertTypeID());
        setPrepareStatement(preStat, 4, tmpMtmUpdateAlertRecipient.getRecipientType());
        setPrepareStatement(preStat, 5, tmpMtmUpdateAlertRecipient.getRecipientID());
        setPrepareStatement(preStat, 6, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 7, new Integer(0));
        setPrepareStatement(preStat, 8, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 9, currTime);
        setPrepareStatement(preStat, 10, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 11, currTime);
        preStat.executeUpdate();
        tmpMtmUpdateAlertRecipient.setID(nextID);
        tmpMtmUpdateAlertRecipient.setCreatorID(sessionContainer.getUserRecordID());
        tmpMtmUpdateAlertRecipient.setCreateDate(currTime);
        tmpMtmUpdateAlertRecipient.setUpdaterID(sessionContainer.getUserRecordID());
        tmpMtmUpdateAlertRecipient.setUpdateDate(currTime);
        tmpMtmUpdateAlertRecipient.setUpdateCount(new Integer(0));
        tmpMtmUpdateAlertRecipient.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getCreatorID()));
        tmpMtmUpdateAlertRecipient.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getUpdaterID()));
        return(tmpMtmUpdateAlertRecipient);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  public synchronized AbstractBaseObject updateByID(Integer ID) throws ApplicationException {
	    PreparedStatement preStat = null;
	    StringBuffer sqlStat = new StringBuffer();
	    MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = new MtmUpdateAlertRecipient();

	    synchronized(dbConn) {
	      try {
	        int updateCnt = 0;
	        Timestamp currTime = Utility.getCurrentTimestamp();
	        sqlStat.append("UPDATE MTM_UPDATE_ALERT_RECIPIENT ");
	        sqlStat.append("SET RECORD_STATUS=? ");
	        sqlStat.append("WHERE  ID=? ");
	        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
	        setPrepareStatement(preStat, 2, ID);
	        updateCnt = preStat.executeUpdate();
	        if (updateCnt == 0) {
	          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
	        } else {
	          tmpMtmUpdateAlertRecipient=(MtmUpdateAlertRecipient)this.getByID(ID);
	          tmpMtmUpdateAlertRecipient.setUpdaterID(sessionContainer.getUserRecordID());
	          tmpMtmUpdateAlertRecipient.setUpdateDate(currTime);
	          tmpMtmUpdateAlertRecipient.setUpdateCount(new Integer(tmpMtmUpdateAlertRecipient.getUpdateCount().intValue() + 1));
	          tmpMtmUpdateAlertRecipient.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getCreatorID()));
	          tmpMtmUpdateAlertRecipient.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getUpdaterID()));
	          return(tmpMtmUpdateAlertRecipient);
	        }
	      } catch (ApplicationException appEx) {
	        throw appEx;
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
	      } finally {
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	      }
	    }
	  } 
  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = (MtmUpdateAlertRecipient)((MtmUpdateAlertRecipient)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE MTM_UPDATE_ALERT_RECIPIENT ");
        sqlStat.append("SET  UPDATE_ALERT_ID=?, UPDATE_ALERT_TYPE_ID=?, RECIPIENT_TYPE=?, RECIPIENT_ID=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmUpdateAlertRecipient.getUpdateAlertID());
        setPrepareStatement(preStat, 2, tmpMtmUpdateAlertRecipient.getUpdateAlertTypeID());
        setPrepareStatement(preStat, 3, tmpMtmUpdateAlertRecipient.getRecipientType());
        setPrepareStatement(preStat, 4, tmpMtmUpdateAlertRecipient.getRecipientID());
        setPrepareStatement(preStat, 5, new Integer(tmpMtmUpdateAlertRecipient.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 6, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 7, currTime);
        setPrepareStatement(preStat, 8, tmpMtmUpdateAlertRecipient.getID());
        setPrepareStatement(preStat, 9, tmpMtmUpdateAlertRecipient.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpMtmUpdateAlertRecipient.setUpdaterID(sessionContainer.getUserRecordID());
          tmpMtmUpdateAlertRecipient.setUpdateDate(currTime);
          tmpMtmUpdateAlertRecipient.setUpdateCount(new Integer(tmpMtmUpdateAlertRecipient.getUpdateCount().intValue() + 1));
          tmpMtmUpdateAlertRecipient.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getCreatorID()));
          tmpMtmUpdateAlertRecipient.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getUpdaterID()));
          return(tmpMtmUpdateAlertRecipient);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = (MtmUpdateAlertRecipient)((MtmUpdateAlertRecipient)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpMtmUpdateAlertRecipient.getID());
        setPrepareStatement(preStat, 2, tmpMtmUpdateAlertRecipient.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpMtmUpdateAlertRecipient);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = (MtmUpdateAlertRecipient)this.oldValue;

    if (tmpMtmUpdateAlertRecipient != null) {
      oldValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getID()));
      oldValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getUpdateAlertID()));
      oldValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getRecipientType()));
      oldValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getRecipientID()));
      oldValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getUpdateDate()));
    }

    tmpMtmUpdateAlertRecipient = (MtmUpdateAlertRecipient)obj;
    if (tmpMtmUpdateAlertRecipient != null) {
      newValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getID()));
      newValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getUpdateAlertID()));
      newValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getRecipientType()));
      newValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getRecipientID()));
      newValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpMtmUpdateAlertRecipient.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/
  public synchronized void deleteByUpdateAlertTypeID(Integer updateAlertTypeID) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT ");
        sqlStat.append("WHERE  UPDATE_ALERT_TYPE_ID=? AND CREATOR_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, updateAlertTypeID);
        setPrepareStatement(preStat, 2, sessionContainer.getUserRecordID());
        preStat.executeUpdate();
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
 

  public synchronized List getRecipientListByUpdateAlertTypeIDRecipientType(Integer updateAlertTypeID, String recipientType) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List resultList = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT UPDATE_ALERT_ID, UPDATE_ALERT_TYPE_ID, RECIPIENT_TYPE, RECIPIENT_ID, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT ");
        sqlStat.append("WHERE  RECORD_STATUS = ? AND UPDATE_ALERT_TYPE_ID = ? AND RECIPIENT_TYPE = ? AND CREATOR_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, updateAlertTypeID);
        this.setPrepareStatement(preStat, 3, recipientType);
        this.setPrepareStatement(preStat, 4, sessionContainer.getUserRecordID());
        rs = preStat.executeQuery();
        while (rs.next()) {
          String recipientID = TextUtility.formatIntegerObj(getResultSetInteger(rs, "RECIPIENT_ID"));
          resultList.add(recipientID);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return resultList;
  }

  public synchronized List getByUpdateAlertTypeID(Integer updateAlertTypeID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List resultList = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT UPDATE_ALERT_ID, UPDATE_ALERT_TYPE_ID, RECIPIENT_TYPE, RECIPIENT_ID, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT ");
        sqlStat.append("WHERE  UPDATE_ALERT_TYPE_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, updateAlertTypeID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = new MtmUpdateAlertRecipient();
          tmpMtmUpdateAlertRecipient.setUpdateAlertID(getResultSetInteger(rs, "UPDATE_ALERT_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertTypeID(getResultSetInteger(rs, "UPDATE_ALERT_TYPE_ID"));
          tmpMtmUpdateAlertRecipient.setRecipientType(getResultSetString(rs, "RECIPIENT_TYPE"));
          tmpMtmUpdateAlertRecipient.setRecipientID(getResultSetInteger(rs, "RECIPIENT_ID"));
          tmpMtmUpdateAlertRecipient.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUpdateAlertRecipient.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUpdateAlertRecipient.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          resultList.add(tmpMtmUpdateAlertRecipient);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return resultList;
  }

  public synchronized List getUpdateAlertListByUserRecordIDGroupListRoleList(Integer userID, List userGroupList, List userRoleList) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
//        sqlStat.append("SELECT DISTINCT A.UPDATE_ALERT_ID ");
        sqlStat.append("SELECT DISTINCT A.UPDATE_ALERT_TYPE_ID ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT A ");
        sqlStat.append("WHERE (RECIPIENT_TYPE = 'U' AND RECIPIENT_ID = ?)  ");
        if ( (userGroupList!=null) && (userGroupList.size()>0) ){
          for (int i=0; i<userGroupList.size(); i++){
            sqlStat.append("OR (RECIPIENT_TYPE = 'G' AND RECIPIENT_ID = ?)  ");
          }
        }
        if ( (userRoleList!=null) && (userRoleList.size()>0) ) {
          for (int i=0; i<userRoleList.size(); i++){
            sqlStat.append("OR (RECIPIENT_TYPE = 'R' AND RECIPIENT_ID = ?)  ");
          }
        }

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int x=0;
        this.setPrepareStatement(preStat, ++x, userID);
        if (userGroupList.size()!=0) {
          for (int i=0; i<userGroupList.size(); i++){
            UserGroup userGroup = (UserGroup) userGroupList.get(i);
            this.setPrepareStatement(preStat, ++x, userGroup.getID());
          }
        }
        if (userRoleList.size()!=0) {
          for (int i=0; i<userRoleList.size(); i++){
            UserRole userRole = (UserRole) userRoleList.get(i);
            this.setPrepareStatement(preStat, ++x, userRole.getID());
          }
        }

        rs = preStat.executeQuery();
        while (rs.next()) {
          Integer updateAlertID = getResultSetInteger(rs, "UPDATE_ALERT_TYPE_ID");
          result.add(updateAlertID);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * getByUpdateAlertID
   * get thte recipient id list by update alert id
   *
   * @param updateAlertID
   * @return Recipient list
   * @throws ApplicationException
   */
  public synchronized List getByUpdateAlertID(Integer updateAlertID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List resultList = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT UPDATE_ALERT_ID, UPDATE_ALERT_TYPE_ID, RECIPIENT_TYPE, RECIPIENT_ID, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT ");
        sqlStat.append("WHERE  UPDATE_ALERT_ID = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, updateAlertID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = new MtmUpdateAlertRecipient();
          tmpMtmUpdateAlertRecipient.setUpdateAlertID(getResultSetInteger(rs, "UPDATE_ALERT_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertTypeID(getResultSetInteger(rs, "UPDATE_ALERT_TYPE_ID"));
          tmpMtmUpdateAlertRecipient.setRecipientType(getResultSetString(rs, "RECIPIENT_TYPE"));
          tmpMtmUpdateAlertRecipient.setRecipientID(getResultSetInteger(rs, "RECIPIENT_ID"));
          tmpMtmUpdateAlertRecipient.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUpdateAlertRecipient.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUpdateAlertRecipient.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          resultList.add(tmpMtmUpdateAlertRecipient);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
    return resultList;
  }

  public synchronized List getUpdateAlertIDListByUserRecordIDGroupListRoleList(Integer userID, List userGroupList, List userRoleList) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT DISTINCT A.UPDATE_ALERT_ID ");
//        sqlStat.append("SELECT DISTINCT A.UPDATE_ALERT_TYPE_ID ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT A ");
        sqlStat.append("WHERE (RECIPIENT_TYPE = 'U' AND RECIPIENT_ID = ?)  ");
        if ( (userGroupList!=null) && (userGroupList.size()>0) ){
          for (int i=0; i<userGroupList.size(); i++){
            sqlStat.append("OR (RECIPIENT_TYPE = 'G' AND RECIPIENT_ID = ?)  ");
          }
        }
        if ( (userRoleList!=null) && (userRoleList.size()>0) ) {
          for (int i=0; i<userRoleList.size(); i++){
            sqlStat.append("OR (RECIPIENT_TYPE = 'R' AND RECIPIENT_ID = ?)  ");
          }
        }

        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int x=0;
        this.setPrepareStatement(preStat, ++x, userID);
        if (userGroupList.size()!=0) {
          for (int i=0; i<userGroupList.size(); i++){
            UserGroup userGroup = (UserGroup) userGroupList.get(i);
            this.setPrepareStatement(preStat, ++x, userGroup.getID());
          }
        }
        if (userRoleList.size()!=0) {
          for (int i=0; i<userRoleList.size(); i++){
            UserRole userRole = (UserRole) userRoleList.get(i);
            this.setPrepareStatement(preStat, ++x, userRole.getID());
          }
        }

        rs = preStat.executeQuery();
        while (rs.next()) {
          Integer updateAlertID = getResultSetInteger(rs, "UPDATE_ALERT_ID");
          result.add(updateAlertID);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  //king--add---start
  public synchronized void deleteByID(Integer ID) throws ApplicationException {
	    PreparedStatement preStat = null;
	    StringBuffer sqlStat = new StringBuffer();

	    synchronized(dbConn) {
	      try {
	        sqlStat.append("DELETE ");
	        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT ");
	        sqlStat.append("WHERE  ID=? ");
	        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
	        setPrepareStatement(preStat, 1, ID);
	        preStat.executeUpdate();
	      } catch (SQLException sqle) {
	        log.error(sqle, sqle);
	        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
	      } catch (Exception e) {
	        log.error(e, e);
	        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
	      } finally {
	        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
	      }
	    }
	  }
  //king--add---end
  
  /**
   * get user alert recipients by dms_document id, update_alert_system_log id and user login information. 
   * lee add for bug 6022 in 06/07/31
   * @param docId
   * @param sysLogId
   * @return
   * @throws ApplicationException
   */
  public synchronized List getInvitationList(
      Integer documentId,
      Integer updateAlertId, 
      Integer userRecordId,
      List userRoles,
      List userGroups) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.UPDATE_ALERT_ID, A.UPDATE_ALERT_TYPE_ID, A.RECIPIENT_TYPE, A.RECIPIENT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT A, UPDATE_ALERT B, UPDATE_ALERT_TYPE C ");
        sqlStat.append("WHERE A.UPDATE_ALERT_ID=B.ID AND A.UPDATE_ALERT_TYPE_ID=C.ID ");
        sqlStat.append(" AND B.ID=C.UPDATE_ALERT_ID ");
        sqlStat.append(" AND A.RECORD_STATUS = 'A' ");
        sqlStat.append(" AND A.UPDATE_ALERT_ID = ? ");
        sqlStat.append(" AND (");
        sqlStat.append("(A.RECIPIENT_TYPE='U' AND A.RECIPIENT_ID = ?) ");
        for (int i=0; (userRoles!=null && i<userRoles.size()); i++) {
          //UserRole userRole = (UserRole)userRoles.get(i);
          sqlStat.append(" OR (A.RECIPIENT_TYPE='R' AND A.RECIPIENT_ID = ?) ");
        } 
        for (int i=0; (userGroups!=null && i<userGroups.size()); i++) {
          //UserGroup userGroup = (UserRole)userGroups.get(i);
          sqlStat.append(" OR (A.RECIPIENT_TYPE='G' AND A.RECIPIENT_ID = ?) ");
        } 
        sqlStat.append(")");
        sqlStat.append(" AND B.RECORD_STATUS='A' AND B.OBJECT_TYPE='D' AND B.OBJECT_ID=? ");
        sqlStat.append(" AND C.ACTION_TYPE='I'");
        //sqlStat.append(" AND C.ACTION_TYPE='I' AND (C.DUE_DATE IS NULL  OR  C.DUE_DATE = '' OR C.DUE_DATE >= ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int counter = 1;
        this.setPrepareStatement(preStat, counter++, updateAlertId);
        this.setPrepareStatement(preStat, counter++, userRecordId);
        for (int i=0; (userRoles!=null && i<userRoles.size()); i++) {
          UserRole userRole = (UserRole)userRoles.get(i);
          this.setPrepareStatement(preStat, counter++, userRole.getID());
        } 
        for (int i=0; (userGroups!=null && i<userGroups.size()); i++) {
          UserGroup userGroup = (UserGroup)userGroups.get(i);
          this.setPrepareStatement(preStat, counter++, userGroup.getID());
        }
        this.setPrepareStatement(preStat, counter++, documentId);
        //this.setPrepareStatement(preStat, counter++, Utility.addDay(Utility.getCurrentTimestamp(),-1));
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = new MtmUpdateAlertRecipient();
          tmpMtmUpdateAlertRecipient.setID(getResultSetInteger(rs, "ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertID(getResultSetInteger(rs, "UPDATE_ALERT_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertTypeID(getResultSetInteger(rs, "UPDATE_ALERT_TYPE_ID"));
          tmpMtmUpdateAlertRecipient.setRecipientType(getResultSetString(rs, "RECIPIENT_TYPE"));
          tmpMtmUpdateAlertRecipient.setRecipientID(getResultSetInteger(rs, "RECIPIENT_ID"));
          tmpMtmUpdateAlertRecipient.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmUpdateAlertRecipient.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmUpdateAlertRecipient.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUpdateAlertRecipient.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUpdateAlertRecipient.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmUpdateAlertRecipient.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getCreatorID()));
          tmpMtmUpdateAlertRecipient.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getUpdaterID()));
          result.add(tmpMtmUpdateAlertRecipient);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * get user alert recipients by dms_document id, update_alert_system_log id and user login information. 
   * lee add for bug 6022 in 06/07/31
   * @param docId
   * @param sysLogId
   * @return
   * @throws ApplicationException
   */
  public synchronized List getList(
      Integer updateAlertId, 
      Integer updateAlertTypeId,
      String recipientType,
      String recordStatus) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.UPDATE_ALERT_ID, A.UPDATE_ALERT_TYPE_ID, A.RECIPIENT_TYPE, A.RECIPIENT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT A ");
        sqlStat.append("WHERE A.ID IS NOT NULL ");
        if (updateAlertId!=null) {
          sqlStat.append(" AND A.UPDATE_ALERT_ID=? ");
        }
        if (updateAlertTypeId!=null) {
          sqlStat.append(" AND A.UPDATE_ALERT_TYPE_ID=? ");
        }
        if (recipientType!=null) {
          sqlStat.append(" AND A.RECIPIENT_TYPE=? ");
        }
        if (recordStatus!=null) {
          sqlStat.append(" AND A.RECORD_STATUS=? ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int counter = 1;
        if (updateAlertId!=null) {
          this.setPrepareStatement(preStat, counter++, updateAlertId);
        }
        if (updateAlertTypeId!=null) {
          this.setPrepareStatement(preStat, counter++, updateAlertTypeId);
        }
        if (recipientType!=null) {
          this.setPrepareStatement(preStat, counter++, recipientType);
        }
        if (recordStatus!=null) {
          this.setPrepareStatement(preStat, counter++, recordStatus);
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          MtmUpdateAlertRecipient tmpMtmUpdateAlertRecipient = new MtmUpdateAlertRecipient();
          tmpMtmUpdateAlertRecipient.setID(getResultSetInteger(rs, "ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertID(getResultSetInteger(rs, "UPDATE_ALERT_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateAlertTypeID(getResultSetInteger(rs, "UPDATE_ALERT_TYPE_ID"));
          tmpMtmUpdateAlertRecipient.setRecipientType(getResultSetString(rs, "RECIPIENT_TYPE"));
          tmpMtmUpdateAlertRecipient.setRecipientID(getResultSetInteger(rs, "RECIPIENT_ID"));
          tmpMtmUpdateAlertRecipient.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpMtmUpdateAlertRecipient.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpMtmUpdateAlertRecipient.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpMtmUpdateAlertRecipient.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpMtmUpdateAlertRecipient.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpMtmUpdateAlertRecipient.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpMtmUpdateAlertRecipient.setCreatorName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getCreatorID()));
          tmpMtmUpdateAlertRecipient.setUpdaterName(UserInfoFactory.getUserFullName(tmpMtmUpdateAlertRecipient.getUpdaterID()));
          result.add(tmpMtmUpdateAlertRecipient);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
/**
 * 
 * @param updateAlertId
 * @param updateAlertTypeId
 * @param recipientType
 * @param recordStatus
 * @return
 * @throws ApplicationException
 */
  public List getRecipientIDList(Integer updateAlertId, Integer updateAlertTypeId, String recipientType, String recordStatus) throws ApplicationException {

    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.UPDATE_ALERT_ID, A.UPDATE_ALERT_TYPE_ID, A.RECIPIENT_TYPE, A.RECIPIENT_ID, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   MTM_UPDATE_ALERT_RECIPIENT A ");
        if( UpdateAlert.USER_RECIPIENT.equals(recipientType) ) {
          sqlStat.append(",USER_RECORD B ");
        } else if ( UpdateAlert.ROLE_RECIPIENT.equals(recipientType) ) {
          sqlStat.append(",USER_ROLE B ");
        } else if ( UpdateAlert.GROUP_RECIPIENT.equals(recipientType) ) {
          sqlStat.append(",USER_GROUP B ");
        }
        sqlStat.append("WHERE A.ID IS NOT NULL ");
        if (updateAlertId!=null) {
          sqlStat.append(" AND A.UPDATE_ALERT_ID=? ");
        }
        if (updateAlertTypeId!=null) {
          sqlStat.append(" AND A.UPDATE_ALERT_TYPE_ID=? ");
        }
        if (recipientType!=null) {
          sqlStat.append(" AND A.RECIPIENT_TYPE=?");
        }
        if (recordStatus!=null) {
          sqlStat.append(" AND A.RECORD_STATUS=? ");
        }
        if( UpdateAlert.USER_RECIPIENT.equals(recipientType) ) {
          sqlStat.append(" AND B.ID = A.RECIPIENT_ID ORDER BY B.FULL_NAME ");
        } else if ( UpdateAlert.ROLE_RECIPIENT.equals(recipientType) ) {
          sqlStat.append(" AND B.ID = A.RECIPIENT_ID ORDER BY B.ROLE_NAME ");
        } else if ( UpdateAlert.GROUP_RECIPIENT.equals(recipientType) ) {
          sqlStat.append(" AND B.ID = A.RECIPIENT_ID ORDER BY B.GROUP_NAME ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int counter = 1;
        if (updateAlertId!=null) {
          this.setPrepareStatement(preStat, counter++, updateAlertId);
        }
        if (updateAlertTypeId!=null) {
          this.setPrepareStatement(preStat, counter++, updateAlertTypeId);
        }
        if (recipientType!=null) {
          this.setPrepareStatement(preStat, counter++, recipientType);
        }
        if (recordStatus!=null) {
          this.setPrepareStatement(preStat, counter++, recordStatus);
        }
        rs = preStat.executeQuery();
        while (rs.next()) {
          result.add(getResultSetInteger(rs, "RECIPIENT_ID"));
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  
  }
}
